// 进度条控件
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import '../packages/theme-chalk/src/index.scss';

import registerComponents from '../packages';
// 引入挂载到基础数据类型原型上的方法
import './utils/prototype';
import { getThemeClusters, resetThemeColor, changeThemeColor } from './utils/themeColor';

import directives from './directives';
import lang from './locale/lang/zh-CN';

const { version } = require('../package.json');

// 正确处理：`当前已勾选 {num} 条数据` & $t('勾选提示', { num: 6 })
const i18nFallback = (s, r) => {
    if (!r || !['[object Array]', '[object Object]'].includes(Object.prototype.toString.call(r))) return lang[s] || s;
    return (lang[s] || s || '').replace(/\{(\w+)\}/g, (match, k) => (r[k] || r[k] === 0 ? r[k] : match));
};

function install(app, config) {
    const { theme, registerComponent } = config || {};
    if (registerComponent !== false) registerComponents(app);
    directives.install(app);
    // 兼容非国际化项目使用
    app.config.globalProperties.$t = app.config.globalProperties.$t || i18nFallback;
    app.config.globalProperties.$changeThemeColor = changeThemeColor;
    app.config.globalProperties.$resetThemeColor = resetThemeColor;
    app.config.globalProperties.$getThemeClusters = getThemeClusters;
    app.provide('$changeThemeColor', changeThemeColor);
    app.provide('$resetThemeColor', resetThemeColor);
    app.provide('$getThemeClusters', getThemeClusters);
    changeThemeColor(window.localStorage.getItem('current_theme_color') || theme || '#00C4A3');
}

if (window && !window.NProgress) window.NProgress = NProgress;
if (window && !window.sibui) window.sibui = { version, install };

// eslint-disable-next-line
console.log(`欢迎使用 sibionics-ui-next v${version}`);

export default {
    version,
    install,
};
